package interceptor;

import java.util.Map;

import model.User;

import action.BaseAction;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class LoginInterceptor implements Interceptor {
    private static final long serialVersionUID = 1L;
    
    public String intercept(ActionInvocation invocation) throws Exception {
    	Map<String, Object> session = invocation.getInvocationContext().getSession();
        User user = (User) session.get("user");
        if (user == null) {
        	System.out.println("no puede pasar");
            return Action.LOGIN;
        } else {
            BaseAction action = (BaseAction) invocation.getAction();
            System.out.println("se chequean los permisos del action con el usuario");
            if (action.tienePermisos(user)){
            	System.out.println("el usuario tiene permiso para acceder");
            	return invocation.invoke();
            }else{
            	System.out.println("el usuario NO tiene permiso para acceder");
            	return Action.LOGIN;
            }
            
        }
    }
 
    public void destroy() {
        System.out.println("Destroying MyLoggingInterceptor...");
    }
    public void init() {
        System.out.println("Initializing MyLoggingInterceptor...");
    }
}
